class App
	def initialize(log, config)
		@log = log
		@config = config
	end
	def run
		@config.data['sources'].each do |src|
			extension = src.sub(/.*\./, '')
			command = @config.data['exec'][extension]
      if(File.exists?(src) == false)
        @log.error "File does not exist [#{src}]!"
			elsif(command == nil)
				@log.error "Unhandled file extension [#{src}]!"
			else
        @log.debug "Processing file [#{src}]..."
				command = command.sub(/%/, src)
				system command # TODO care about return value?!
        print "Enter aliases (separated with spaces): "
				aliases = gets.chomp.split(" ")
        aliases.each do |aliaz|
          path = @config.data['targets'][aliaz]
          if path == nil
            msg = "Unkown alias [#{aliaz}] entered!"
            @log.error msg
            puts msg
          else
            fileName = src.sub(/.*\//, '')
            dest = path + '/' + fileName
            
            isRename = false
            isCopy = true
            if File.exists?(dest)
              if File.size(src) == File.size(dest)
                isCopy = false
              else
                isRename = true
              end
            end
            
            if isCopy
              if isRename
                dest = _safeName(path, fileName)
              end
              @log.debug "Copying file from [#{src}] to [#{dest}]."
              FileUtils.copy_file(src, dest)
            else
              @log.debug "Skipping same processed file [#{src}]."
            end
          end # if path != nil
        end # aliases.each do
			end # File.exists == true && command != nil
		end # each source
	end # def run
  def _safeName(path, fileName)
    i = 1
    # TODO append counter-suffix in between file name and extension, instead at very end
    newName = path + '/' + fileName + "-" + i.to_s
    while (File.exists?(newName) == true) do
      i++
      newName = path + '/' + fileName + "-" + i.to_s
    end
    return path + '/' + fileName + "-" + i.to_s
  end
  
end